package com.cai.server;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.server.ServerEndpoint;
import javax.websocket.Session;
import java.io.IOException;

@Component
@Slf4j
@ServerEndpoint("/server/{user}")
public class WebSocketServer {
    @OnOpen
    public void onOpen(Session session){
         log.debug("连接成功");
    }
    @OnClose
    public void onClose(Session session){
        log.debug("关闭连接");
    }
    @OnMessage
    public void onMessage(Session session,String message) throws IOException {
        log.info("客户端：{}",message);
        //给客户端发送消息
        session.getBasicRemote().sendText(message);
    }
}
